<template>
  <div id="orderRtx" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      :title="$t('out.rtxorder')"
      @exportPo="exportPo"
      @allocation="allocation"
      @grant="grant"
      @revocation="revocation"
      @order="order"
      @deliver="deliver"
      @del="del"
      @createwave="createwave"
      @printJHD="printJHD"
    ></v-tableTitle>
    <v-table
      :pageSize="pageSize"
      :page="page"
      :tableConfig="tableConfig"
      :tableData="tableData"
      :total="total"
      :statusData="sel"
      @findData="getTableData"
      @getTableData="getTableData"
      @searhData="searhData"
      @selectData="selectData"
      @setdialog="setdialog"
      @upData="getTableData"
      ref="table"
    ></v-table>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import bus from "../../../../bus/bus";
import config from "./Config";
import print from "@/assets/js/print";

export default {
  name: "orderRtx",
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('out.orderkey'),
          prop: "orderkey",
          input: "input",
        },
        {
          label: this.$t('out.externorderkey'),
          prop: "externorderkey",
          input: "input",
        },
        {
          label: this.$t('config.shipper'),
          prop: "storerkey",
          input: "input",
        },
        {
          label: this.$t('in.podate1'),
          prop: "orderdatestr",
          input: "input",
        },
        {
          label: this.$t('in.datereceived'),
          prop: "adddatestr",
          input: "input",
        },
        {
          label: this.$t('out.actualshipdatestr'),
          prop: "actualshipdatestr",
          input: "input",
        },
        {
          label: this.$t('out.originalqty'),
          prop: "totalorders",
          searh: "totalordersstr",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('out.totalqtyallocated'),
          prop: "totalqtyallocated",
          searh: "totalqtyallocatedstr",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('out.totalqtypicked'),
          prop: "totalqtypicked",
          searh: "totalqtypickedstr",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('out.totalshippedqty'),
          prop: "totalshippedqty",
          searh: "totalshippedqtystr",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('out.rtxexternaltype'),
          prop: "rtxexternaltype",
          input: "input",
        },
        {
          selected: true,
          label: this.$t('out.status'),
          prop: "status",
          searh: "statusstr",
          input: "select",
          multiple: "true",
          options: this.getOptions("queryOrderStatusDropDown"),
        },
        {
          label: this.$t('out.rtxsalesp'),
          prop: "rtxsalesp",
          input: "input",
        },
        {
          label: this.$t('out.rtxshopname'),
          prop: "rtxshopname",
          input: "input",
        },
        {
          label: this.$t('out.wavekey'),
          prop: "wavekey",
          input: "input",
        },
        {
          label: this.$t('config.carrierCode'),
          prop: "carriercode",
          input: "input",
        },
        {
          label: this.$t('out.rtxexpresskey'),
          prop: "rtxexpresskey",
          input: "input",
        },
        {
          label: this.$t('out.rtxbuyermsg'),
          prop: "rtxbuyermsg",
          input: "input",
        },

        {
          label: this.$t('out.contact1'),
          prop: "c_contact1",
          input: "input",
        },
        {
          label: this.$t('config.phone'),
          prop: "c_phone2",
          input: "input",
        },
        {
          label: this.$t('out.c_city'),
          prop: "c_city",
          input: "input",
        },
        {
          label: this.$t('out.sellerMessage'),
          prop: "sellerMessage",
          input: "input",
        },
        {
          label: this.$t('out.rtxcancelmark'),
          prop: "rtxcancelmark",
          input: "input",
        },
        {
          label: this.$t('in.receiptfinishflag'),
          prop: "orderfinishflag",
          input: "input",
        },
        {
          label: this.$t('out.msg'),
          prop: "msg",
          input: "input",
        },
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 0,
      tableSelectData: [],
      tableSearhData: {},
      sel: [],
    };
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.export'),
          certigier: this.downloadStatus||!this.btnPermNameList.includes('WM020401'),
          disabled: true,
          invalid: "exportPo",
        },
        /*{ label: this.$t('common.delete'), certigier: this.deleteStatus, disabled: true, invalid: "del" },*/
        {
          label: this.$t('out.createwave'),
          certigier: this.deleteStatus||!this.btnPermNameList.includes('WM020402'),
          disabled: true,
          invalid: "createwave",
        },
        {
          type: "more",
          label: this.$t('common.moreMenu'),
          certigier: !this.btnPermNameList.includes('WM020403'),
          invalid: "down",
          btnConfig: [
            {
              label: this.$t('out.allocation'),
              certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020404'),
              disabled: true,
              invalid: "allocation",
            },
            /*{label: this.$t('out.grant'), certigier: this.fafangStatus, disabled: true, invalid: "grant"},*/
            {
              label: this.$t('out.revocation'),
              certigier: this.deliverStatus||!this.btnPermNameList.includes('WM020405'),
              disabled: true,
              invalid: "revocation",
            },
            {
              label: this.$t('out.allPick'),
              certigier: this.allPickStatus||!this.btnPermNameList.includes('WM020406'),
              disabled: true,
              invalid: "order",
            },
            {
              label: this.$t('out.allShip'),
              certigier: this.allShipStatus||!this.btnPermNameList.includes('WM020407'),
              disabled: true,
              invalid: "deliver",
            },
            {
              label: this.$t('out.printJHD'),
              certigier: this.deleteStatus||!this.btnPermNameList.includes('WM020408'),
              disabled: true,
              invalid: "printJHD",
            },
          ],
        },
      ];
    },
  },
  created() {},
  methods: {
    deliver(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.get(`/ordersDeliverAll/${item.orderkey}`);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.getTableData(page, this.tableSearhData);
          
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$refs.table.delSelectData(item, index);
        }
      });
    },
    order(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.get(`/ordersPickingAll/${item.orderkey}`);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.getTableData(page, this.tableSearhData);
          
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$refs.table.delSelectData(item, index);
        }
      });
    },
    revocation(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.get(
            `/ordersRevokeAllocate/${item.orderkey}`
          );
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.getTableData(page, this.tableSearhData);
          
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$refs.table.delSelectData(item, index);
        }
      });
    },
    allocation(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.post(
            `/allocateByOrderkey/${item.orderkey}/${this.$store.state.wms}`
          );
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.getTableData(page, this.tableSearhData);
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$refs.table.delSelectData(item, index);
        }
      });
    },
    grant(data) {
      data.forEach(async (item, index) => {
        try {
          let res = await this.$api.get(`/ordersReleaseTask/${item.orderkey}`);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem("page");
            this.getTableData(page, this.tableSearhData);
          
        } catch (e) {
          this.$message.error(e);
        } finally {
          this.$refs.table.delSelectData(item, index);
        }
      });
    },
    //导出
    async exportPo(data) {
      try {
        let res = await this.$api.post("/exportOrders", data, {
          responseType: "blob",
        });
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('out.rtxorder') + ".xls");
        } else {
          var blob = new Blob([res.data], {
            type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8",
          });
          var filename = this.$t('out.rtxorder') + ".xls";
          var downloadElement = document.createElement("a");
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = "none";
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      }
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData(page, data) {
      console.log(data);
      try {
        this.loading = true;
        let res = await this.$api.post(`/queryOrdersRtxList/${page}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curpage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.$router.push({ name: "orderRtxDeawer" });
    },
    async del(data) {
      try {
        let res = await this.$api.post(`/deleteOrders`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem("page");
          this.getTableData(page, this.tableSearhData);
          
        
      } catch (e) {
        this.$message.error(e);
      }
    },

    async createwave(data) {
      try {
        let res = await this.$api.post(`/createWave`, data);
        
          this.$message.success(res.returnMgr);
          let page = sessionStorage.getItem("page");
          this.getTableData(page, this.tableSearhData);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async printJHD(data) {
      for (let i = 0; i < data.length; i++) {
        print.bcgrfprint("ORDERJHD", data[i].orderkey);
      }
    },
    // 调用订单状态的接口
    async statusData() {
      let res = await this.$api.get(`/queryOrderStatusDropDownDefault`);
      console.log(res);
      res = res.data;
      this.sel = res;
      console.log(this.sel);
      this.getTableData(1, { statusstr: res });
      bus.$emit("statusData", res);
      // let page = sessionStorage.getItem("page");
      // this.getTableData(page, this.tableSearhData);
    },
  },
  mounted() {
    ;
    this.$store.commit("setSkip", {});
    this.$store.commit("setRouteId", "");
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.$store.commit("setDetailsNew", false);
    this.$store.commit("setTemporary", "");
    // 调用默认订单状态接口
    // 返回后调用
    // this.getTableData(1, { statusstr: ["09"] });
    this.statusData();
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = this.downloadStatus = false;
          this.allocationStatus = this.fafangStatus = false;
          this.deliverStatus = this.allPickStatus = this.allShipStatus = false;
        } else {
          this.deleteStatus = this.downloadStatus = true;
          this.allocationStatus = this.fafangStatus = true;
          this.deliverStatus = this.allPickStatus = this.allShipStatus = true;
        }
      }
    },
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  },
};
</script>

<style scoped>
/* 下拉框的的样式调整 */
div /deep/.el-tag.el-tag--info:nth-child(1) {
  height: 22px;
  width: 112px;
  text-align: center;
}
div /deep/.el-tag.el-tag--info:nth-child(2) {
  height: 22px;
  width: 42px;
}
div /deep/.el-select > .el-input {
  width: 190px;
}
div /deep/.el-select .el-tag {
  justify-content: center;
}
div /deep/.el-select__tags {
  min-width: 180px;
  flex-wrap: nowrap;
  top: 50%;
  font-size: 5px !important;
}

#orderRtx {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
